package com.chuji.crmcj.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.GoodMan;
import com.chuji.crmcj.bean.TeunMagInfo;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.UIUtils;

import java.util.ArrayList;

/**
 * Created by w.pitt on 2016/3/9.
 */
public class TenureMagAdapter extends BaseAdapter {

    private TeunMagInfo mteunMagInfo;
    private Context mContext;
    private ArrayList<GoodMan> mPersons;
    private String mPreFirstPinyin;

    public TenureMagAdapter(Context context, ListView mTenuLv, TeunMagInfo teunMagInfo, ArrayList<GoodMan> mPersons) {
        this.mteunMagInfo = teunMagInfo;
        this.mContext = mContext;
        this.mPersons = mPersons;
    }

    @Override
    public int getCount() {
        return mteunMagInfo.getApiParamObj().isEmpty() ? 0 : mteunMagInfo.getApiParamObj().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private ViewHolder holder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(UIUtils.getContext(), R.layout.item_mag, null);
            holder = new ViewHolder();
            holder.tvPinyin = (TextView) convertView.findViewById(R.id.tv_pinyin);
            holder.tv_client_name = (TextView) convertView.findViewById(R.id.tv_client_name);
            holder.tv_salesman_name = (TextView) convertView.findViewById(R.id.tv_salesman_name);
            holder.tv_client_num = (TextView) convertView.findViewById(R.id.tv_client_num);
            holder.tv_car_type = (TextView) convertView.findViewById(R.id.tv_car_type);
            holder.tv_want_car = (TextView) convertView.findViewById(R.id.tv_want_car);
            holder.tv_handle_time = (TextView) convertView.findViewById(R.id.tv_handle_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_client_name.setText(mteunMagInfo.getApiParamObj().get(position).getName());
        holder.tv_salesman_name.setText(mteunMagInfo.getApiParamObj().get(position).getRealName());
        if (mteunMagInfo.getApiParamObj().get(position).getTel().length() == 11) {
            holder.tv_client_num.setText(NumberUtils.parsePhoneNumber(mteunMagInfo.getApiParamObj().get(position).getTel()));
        }
        holder.tv_car_type.setText(mteunMagInfo.getApiParamObj().get(position).getCarModel());
        holder.tv_want_car.setText("成交车型");
        holder.tv_handle_time.setText(mteunMagInfo.getApiParamObj().get(position).getLastTime());

        GoodMan goodMan = mPersons.get(position);
        String mFirstPinyin = String.valueOf(goodMan.mPinyin.charAt(0));
        if (position == 0) {
            mPreFirstPinyin = "-";
        } else {
            mPreFirstPinyin = String.valueOf(mPersons.get(position - 1).mPinyin.charAt(0));
        }
        holder.tvPinyin.setVisibility(TextUtils.equals(mPreFirstPinyin, mFirstPinyin) ? View.GONE
                : View.VISIBLE);
        holder.tvPinyin.setText(String.valueOf(goodMan.mPinyin.charAt(0)));

        return convertView;
    }

    static class ViewHolder {
        private TextView tv_client_name;
        private TextView tv_salesman_name;
        private TextView tv_client_num;
        private TextView tv_car_type;
        private TextView tv_want_car;
        private TextView tv_handle_time;
        private TextView tvPinyin;
    }

}
